import com.sun.jndi.ldap.pool.Pool;
import sun.java2d.pipe.SpanIterator;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-03-25
 * Time: 22:41
 */
public class MyThreadPool {
    private BlockingQueue<Runnable> pool = new LinkedBlockingQueue<>();

    public void submit(Runnable runnable) throws InterruptedException {
        pool.put(runnable);
    }

    //实现固定线程数的线程池
    //不是容量，是确确实实的线程数
    public MyThreadPool(int number) {
        for (int i = 0; i < number; i++) {
            Thread thread = new Thread(() -> {
                Runnable runnable = null;
                try {
                    while (true) {
                        runnable = pool.take();
                        runnable.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
    }


    public static void main1(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(10);
        for (int i = 1; i <= 1000; i++) {
            int id = i; //线程id，变量捕获~
            myThreadPool.submit(() -> {
                System.out.println("好耶^ v ^ " + id);
            });
        }
        //在阻塞中，因为线程池中没有一个线程抢到任务去执行了~
    }
}
